<?php



$str = 'abc|ser|Abc|a bc|abcd';
$str = '天下武|天下|天 下|天下1|开心';//中文词

/**
 * 格式化匹配用的正则字符串
 * @param $str
 * @return string[]
 */
function format($str) {
	$str = preg_replace('~\s+~', '', $str);
	$arr = explode('|',$str);
	$arr = array_map('strtolower', $arr);//字母统一小写
	$arr = array_unique($arr); //去重
	$arr = array_filter($arr); //去空
	return $arr;

}

/**
 * 自身能被自身多次匹配的字符串要去掉 新的字符串是不是在已存在的正则，或者正则有被包含在字符串里
 * 过滤自身 方式1 strpos
 * @param $arr
 * @return array
 */

function selfFilter($arr) {
	sort($arr);
	$prev = null;
	$stand = [];
	foreach ($arr  as $v) {
		//var_dump($v, $prev,strpos($v, $prev));
		if(!$prev || false === strpos($v, $prev)) {
			foreach ($stand as $val) {
				//正则某串包含某字符串里(尾部追加的处理)
				if ( false !== strpos($val, $v)) {
					var_dump($v,$val,strpos($val, $v));
					unset($stand[$val]);
					//$stand[$v] = $prev = $v;
				}
				//某字符串包含在正则里
				if ( false !== strpos($v,$val)) {
					var_dump('exist', $v,$val);
					continue 2;
				}
			}
			$stand[$prev] = $prev = $v;
		} else {
			var_dump('prev', $v, $prev);
		}
	}
	return $stand;
}


//过滤自身 方式2 正则
function selfRegularfilter($arr) {
	sort($arr);
	$regular = [];
	$prev = null;
	$str = implode('|', $arr);
	foreach ($arr as $v){
		if (!$prev) {
			$regular[] = $prev  = $v;
		    continue;
		}
		/*if(mb_eregi($str, $v, $match) && $prev != $match[0]) {
			$regular[] = $prev = $match[0];
		}*/
		//1.新的字符串不在已存在的正则，2.正则有被包含在字符串里
		$tmpReg = implode('|', $regular);
		if ($tmpReg && !mb_eregi($tmpReg, $v, $match)) {
			if (mb_eregi($v, $tmpReg, $match)) {

			}
			$regular[] = $prev  = $v;
		}

	}
	return $regular;
}


//自身过滤
$str = file_get_contents('1.txt');
$arr = explode('|', $str);
$classify = include "bad2.php";
/*$str = $classify['porn'];
*/
/*$arr = format($str);
$regular = selfFilter($arr);
//$regular1 = selfRegularFilter($arr);
//$diff = array_diff($regular1, $regular);
//$diff1 = array_diff($regular, $regular1);
$newPreg = implode('|', $regular);
//var_dump($diff, $diff1);
echo $newPreg;*/

/*foreach ($classify as $k => $v) {
	$str = $v;
    $arr = format($str);
    $regular = selfFilter($arr);
    $newPreg = implode('|', $regular); 
    //echo $k.'：'. $newPreg, "<br/>\n";
}*/

//过滤重复
function diff($preg, $arr)
{
	$stand = [];
	foreach ($arr as $v) {
		if (
			!($rt=mb_eregi($preg, $v, $match))

	    ) {
			//var_dump($v);
			$stand[] = $v;
		} else {
			var_dump($rt,$v,$match);
		}
	}
	return $stand;
}

/*$preg = $classify['corrupted'];
$arr = explode('|', $classify['liveihood']);
$diff = diff($preg, $arr);
$newPreg = implode('|', $diff); 
echo $newPreg;
*/

foreach ($classify as $k => $v) {
	if ($v && 'porn' != $k) {
		$arr =  explode('|', $v);
		foreach ($classify as $ke => $va) {
			if ($va && $k != $ke) {
				$newPreg  = $va;
               //$arr = explode('|', $va);
			   $arr = diff($newPreg, $arr);
			   
			}
	    }
	    $newPreg = implode('|', $arr); 
	    //var_dump($k);
	    echo $k . '：'. $newPreg, "<br/>\n";
	}
}

/*foreach ($classify as $k => $v) {
	if ($v) {
		$preg = $v;
		$arr = diff($preg, $arr);
        //$newPreg = implode('|', $diff); 
	    //var_dump($k);
	}
}
$newPreg = implode('|', $arr); 
 echo  $newPreg, "<br/>\n";
*/

/*$v = 'smile';
$preg = 'sm(?=[^a-z])';
$preg1 = '~sm(?![a-z])~';
$rt=mb_eregi($preg, $v, $match);
//$rt=preg_match($preg1, $v, $match);
var_dump($rt, $v, $match);*/

